
create table danhmuc
(
madanhmuc int identity(1,1),
tendanhmuc nvarchar(255),
primary key (madanhmuc)
)

create table diadiem
(
madiadiem int identity(1,1),
tendiadiem nvarchar(255),
diachi nvarchar(255),
sodienthoai nvarchar(30),
email nvarchar(255),
primary key (madiadiem)
)

create table diemden
(
madiemden int identity(1,1),
tendiemden nvarchar(255),
primary key (madiemden)
)

create table donhang
(
madonhang int identity(1,1),
masanpham int,
manguoidung int,
mahinhthucthanhtoan int,
matinhtrang int,
diachigiaohang nvarchar(255),
ngaygiodathang datetime,
ngaygiohuy datetime,
hotenkhachhang nvarchar(255),
diachikhachhang nvarchar(255),
emailkhachhang nvarchar(255),
dienthoaikhachhang nvarchar(30),
soluong int,
tongtien float,
primary key(madonhang)
)
alter table donhang
add constraint pk_donhang_sp foreign key (masanpham) references sanpham(masanpham)

alter table donhang
add constraint pk_donhang_ngdung foreign key (manguoidung) references nguoidung(manguoidung)

alter table donhang
add constraint pk_donhang_httt foreign key (mahinhthucthanhtoan) references hinhthucthanhtoan(mahinhthucthanhtoan)

alter table donhang
add constraint pk_donhang_matt foreign key (matinhtrang) references tinhtrangdonhang(matinhtrang)



create table dulich
(
masanpham int identity(1,1),
madiemden int,
makhoanggia int,
nhacungcap nvarchar(255),
primary key (masanpham),
)
alter table dulich
add constraint pk_dulich_madd foreign key (madiemden) references diemden(madiemden)

alter table dulich
add constraint pk_dulich_makg foreign key (makhoanggia) references khoanggia(makhoanggia)

create table emailthongbao
(
mathongbao int identity(1,1),
email nvarchar(255),
huy int,
primary key (mathongbao)
)

create table hinhthucthanhtoan
(
mahinhthucthanhtoan int identity(1,1),
tenhinhthucthanhtoan nvarchar(255),
primary key (mahinhthucthanhtoan)
)

create table khoanggia
(
makhoanggia int identity(1,1),
tenkhoanggia nvarchar(255),
giatoithieu int,
giatoida int,
primary key (makhoanggia)
)

create table lichsuvi
(
magiaodich int identity(1,1),
manguoidung int,
maloaigiaodich int,
sotien int,
ngaygio datetime,
primary key (magiaodich)
)
alter table lichsuvi
add constraint pk_lichsuvi_mand foreign key (manguoidung) references nguoidung(manguoidung)

alter table lichsuvi
add constraint pk_lichsuvi_malgd foreign key (maloaigiaodich) references loaigiaodich(maloaigiaodich)

create table loaigiaodich
(
maloaigiaodich int identity(1,1),
tenloaigiaodich nvarchar(255),
primary key(maloaigiaodich)
)

create table loainguoidung
(
maloainguoidung int identity(1,1),
tenloainguoidung nvarchar(255),
primary key(maloainguoidung)
)

create table nguoidung
(
manguoidung int identity(1,1),
maloainguoidung int,
emaildangnhap nvarchar(255),
matkhau nvarchar(255),
sodienthoai nvarchar(30),
diachigiaohang nvarchar(255),
kichhoatdienthoai int,
tongtientrongvi int,
hinhdaidien nvarchar(255),
gioitinh bit,
primary key(manguoidung)
)
alter table nguoidung
add constraint pk_nguoidung_malnd foreign key (maloainguoidung) references loainguoidung(maloainguoidung)


create table phanquyen
(
maphanquyen int identity(1,1),
maloainguoidung int,
primary key (maphanquyen)
)

create table quydinh
(
slphieutoidachomotsanpham int identity(1,1),
phihoantienbanggold int,
phihoantienbangtienmat int,
thoigiangiaohangtoithieu int,
thoigiangiaohangtoida int,
doigoldthanhtien int,
sotiennaptoithieu int
)
create table sanpham
(
masanpham int identity(1,1),
madanhmuc int,
madiadiem int,
matinhtrangsanpham int,
tensanpham nvarchar(255),
chitiet nvarchar(255),
giagoc int,
tilegiam int,
giamgia int,
phigiaohangtannoi int,
ngaygiobatdau datetime,
ngaygioketthuc datetime,
luotquangcao int,
dohapdan int,
hinhanh nvarchar(255),
slphieutoithieu int,
slphieutoida int,
hansudung datetime,
primary key (masanpham)
)
alter table sanpham
add constraint pk_sanpham_madm foreign key (madanhmuc) references danhmuc(madanhmuc)

alter table sanpham
add constraint pk_sanpham_madd foreign key (madiadiem) references diadiem(madiadiem)

alter table sanpham
add constraint pk_sanpham_mattsp foreign key (matinhtrangsanpham) references tinhtrangsanpham(matinhtrangsanpham)



create table tinhthanh
(
matinh int identity(1,1),
tentinh nvarchar(255),
primary key (matinh)
)
create table tinhtrangdonhang
(
matinhtrang int identity (1,1),
tentinhtrang nvarchar(255),
primary key(matinhtrang)
)
create table tinhtrangsanpham
(
matinhtrangsanpham int identity(1,1),
tentinhtrang nvarchar(255),
primary key(matinhtrangsanpham)
)

--******procedure ********----
create procedure danhmuc_Insert
	@madanhmuc int,
	@tendanhmuc nvarchar(255)
as
	begin 
		insert into danhmuc
		values(@tendanhmuc)
	end 
create proc danhmuc_Update
	@madanhmuc int,
	@tendanhmuc nvarchar(255)
as
	begin
		update danhmuc
		set tendanhmuc=@tendanhmuc
		where madanhmuc=@madanhmuc
	end
CREATE PROC danhmuc_Delete
	@madanhmuc	int
AS
	DELETE danhmuc WHERE madanhmuc = @madanhmuc
--------danh muc get------------
create procedure danhmuc_get
	@Where NVARCHAR(255),
	@PageIndex INT,
	@PageSize INT,
	@OrderBy NVARCHAR(25),
	@OrderDirection NVARCHAR(4),
	@TotalRecords INT OUTPUT
as 
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
CREATE TABLE #Temp (
IDTemp INT IDENTITY(1,1),
madanhmuc INT
)
DECLARE @sql NVARCHAR(1000)
SET @sql = 'INSERT INTO #temp ([madanhmuc]) SELECT [madanhmuc] FROM danhmuc ' + @Where + ' ORDER BY ' + @OrderBy + ' ' + @OrderDirection
EXEC (@sql)
SELECT @TotalRecords = COUNT(*) FROM #Temp
SELECT
	S.madanhmuc,
	S.tendanhmuc
FROM #Temp AS T JOIN danhmuc AS S ON T.madanhmuc = S.madanhmuc
WHERE
(@PageIndex = 0)
OR
(T.IDTemp > (@PageIndex - 1) * @PageSize AND
T.IDTemp <= @PageIndex * @PageSize)
DROP TABLE #temp
----------dia diem procedure----------
create procedure diadiem_Insert
	@madiadiem int,
	@tendiadiem nvarchar(255),
	@diachi nvarchar(255),
	@sodienthoai nvarchar(255),
	@email nvarchar(255)
as
	begin 
		insert into diadiem
		values(@tendiadiem,@diachi,@sodienthoai,@email)
	end 

create proc diadiem_Update
	@madiadiem int,
	@tendiadiem nvarchar(255),
	@diachi nvarchar(255),
	@sodienthoai nvarchar(255),
	@email nvarchar(255)
as
	begin
		update diadiem
		set tendiadiem=@tendiadiem
		where madiadiem=@madiadiem
	end

CREATE PROC diadiem_delete
	@madiadiem	int
AS
	DELETE diadiem WHERE madiadiem = @madiadiem

drop procedure Proc_Delete_diadiem
--------dia diem get------------
create procedure diadiem_get
	@Where NVARCHAR(255),
	@PageIndex INT,
	@PageSize INT,
	@OrderBy NVARCHAR(25),
	@OrderDirection NVARCHAR(4),
	@TotalRecords INT OUTPUT
as 
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
CREATE TABLE #Temp (
IDTemp INT IDENTITY(1,1),
madiadiem INT
)
DECLARE @sql NVARCHAR(1000)
SET @sql = 'INSERT INTO #temp ([madiadiem]) SELECT [madiadiem] FROM diadiem ' + @Where + ' ORDER BY ' + @OrderBy + ' ' + @OrderDirection
EXEC (@sql)
SELECT @TotalRecords = COUNT(*) FROM #Temp
SELECT
	S.madiadiem,
	S.tendiadiem,
	S.diachi,
	S.sodienthoai,
	S.email
FROM #Temp AS T JOIN diadiem AS S ON T.madiadie = S.madiadiem
WHERE
(@PageIndex = 0)
OR
(T.IDTemp > (@PageIndex - 1) * @PageSize AND
T.IDTemp <= @PageIndex * @PageSize)
DROP TABLE #temp
------diem den pro------------
create procedure diemden_Insert
	@madiemden int,
	@tendiemden nvarchar(255)
as
	begin 
		insert into diemden
		values(@tendiemden)
	end 

create proc diemden_Update
	@madiemden int,
	@tendiemden nvarchar(255)
as
	begin
		update diemden
		set tendiemden=@tendiemden
		where madiemden=@madiemden
	end

CREATE PROC diemden_delete
	@madiemden	int
AS
	DELETE diemden WHERE madiemden = @madiemden
--------diem den get------------
create procedure diemden_get
	@Where NVARCHAR(255),
	@PageIndex INT,
	@PageSize INT,
	@OrderBy NVARCHAR(25),
	@OrderDirection NVARCHAR(4),
	@TotalRecords INT OUTPUT
as 
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
CREATE TABLE #Temp (
IDTemp INT IDENTITY(1,1),
madiemden INT
)
DECLARE @sql NVARCHAR(1000)
SET @sql = 'INSERT INTO #temp ([madiemden]) SELECT [madiemden] FROM diemden ' + @Where + ' ORDER BY ' + @OrderBy + ' ' + @OrderDirection
EXEC (@sql)
SELECT @TotalRecords = COUNT(*) FROM #Temp
SELECT
	S.madiemden,
	S.tendiemden
FROM #Temp AS T JOIN diadiem AS S ON T.madiemden = S.madiemden
WHERE
(@PageIndex = 0)
OR
(T.IDTemp > (@PageIndex - 1) * @PageSize AND
T.IDTemp <= @PageIndex * @PageSize)
DROP TABLE #temp
----don hang pro------
create procedure donhang_Insert
	@madonhang int,
	@masanpham int,
	@manguoidung int,
	@mahinhthucthanhtoan int,
	@matinhtrang int,
	@diachigiaohang nvarchar(255),
	@ngaygiodathang datetime,
	@ngaygiohuy datetime,
	@hotenkhachhang nvarchar(255),
	@diachikhachhang nvarchar(255),
	@emailkhachhang nvarchar(255),
	@dienthoaikhachhang nvarchar(255),
	@soluong int,
	@tongtien float
as
	begin 
		insert into donhang
		values(@masanpham,@manguoidung,@mahinhthucthanhtoan,@matinhtrang,@diachigiaohang,@ngaygiodathang,@ngaygiohuy,@hotenkhachhang,
				@diachikhachhang,@emailkhachhang,@dienthoaikhachhang,@soluong,@tongtien)
	end 

create proc donhang_Update
	@madonhang int,
	@masanpham int,
	@manguoidung int,
	@mahinhthucthanhtoan int,
	@matinhtrang int,
	@diachigiaohang nvarchar(255),
	@ngaygiodathang datetime,
	@ngaygiohuy datetime,
	@hotenkhachhang nvarchar(255),
	@diachikhachhang nvarchar(255),
	@emailkhachhang nvarchar(255),
	@dienthoaikhachhang nvarchar(255),
	@soluong int,
	@tongtien float
as
	begin
		update donhang
		set masanpham=@masanpham
		where madonhang=@madonhang
	end
---
CREATE PROC donhang_delete
	@madonhang	int
AS
	DELETE donhang WHERE madonhang = @madonhang
--------don hang get------------
create procedure donhang_get
	@Where NVARCHAR(255),
	@PageIndex INT,
	@PageSize INT,
	@OrderBy NVARCHAR(25),
	@OrderDirection NVARCHAR(4),
	@TotalRecords INT OUTPUT
as 
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
CREATE TABLE #Temp (
IDTemp INT IDENTITY(1,1),
madonhang INT
)
DECLARE @sql NVARCHAR(1000)
SET @sql = 'INSERT INTO #temp ([madonhang]) SELECT [madonhang] FROM donhang ' + @Where + ' ORDER BY ' + @OrderBy + ' ' + @OrderDirection
EXEC (@sql)
SELECT @TotalRecords = COUNT(*) FROM #Temp
SELECT
	S.madonhang,
	S.masanpham,
	S.manguoidung,
	S.mahinhthucthanhtoan,
	S.matinhtrang,
	S.diachigiaohang,
	S.ngaygiodathang,
	S.ngaygiohuy,
	S.hotenkhachhang,
	S.diachikhachhang,
	S.emailkhachhang,
	S.dienthoaikhachhang,
	S.soluong,
	S.tongtien
FROM #Temp AS T JOIN donhang AS S ON T.madonhang = S.madonhang
WHERE
(@PageIndex = 0)
OR
(T.IDTemp > (@PageIndex - 1) * @PageSize AND
T.IDTemp <= @PageIndex * @PageSize)
DROP TABLE #temp
----du lich pro-------
create procedure dulich_Insert
	@masanpham int,
	@madiemden int,
	@makhoanggia int,
	@nhacungcap nvarchar(255)
as
	begin 
		insert into dulich
		values(@madiemden,@makhoanggia,@nhacungcap)
	end 

create proc dulich_Update
	@masanpham int,
	@madiemden int,
	@makhoanggia int,
	@nhacungcap nvarchar(255)
as
	begin
		update dulich
		set madiemden=@madiemden
		where masanpham=@masanpham
	end

CREATE PROC ducclich_delete
	@masanpham	int
AS
	DELETE dulich WHERE masanpham = @masanpham
--------du lich get------------
create procedure dulich_get
	@Where NVARCHAR(255),
	@PageIndex INT,
	@PageSize INT,
	@OrderBy NVARCHAR(25),
	@OrderDirection NVARCHAR(4),
	@TotalRecords INT OUTPUT
as 
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
CREATE TABLE #Temp (
IDTemp INT IDENTITY(1,1),
masanpham INT
)
DECLARE @sql NVARCHAR(1000)
SET @sql = 'INSERT INTO #temp ([masanpham]) SELECT [masnapham] FROM dulich ' + @Where + ' ORDER BY ' + @OrderBy + ' ' + @OrderDirection
EXEC (@sql)
SELECT @TotalRecords = COUNT(*) FROM #Temp
SELECT
	S.masanpham,
	S.madiemden,
	S.makhoanggia,
	S.nhacungcap
FROM #Temp AS T JOIN dulich AS S ON T.masanpham = S.masanpham
WHERE
(@PageIndex = 0)
OR
(T.IDTemp > (@PageIndex - 1) * @PageSize AND
T.IDTemp <= @PageIndex * @PageSize)
DROP TABLE #temp
-------cua thach---------
-------hinh thuc thanh toan--------
--------insert---------
create procedure hinhthucthanhtoan_Insert
	@mahinhthucthanhtoan int,
	@tenhinhthucthanhtoan nvarchar(255)
as
	begin 
		insert into hinhthucthanhtoan
		values(@tenhinhthucthanhtoan)
	end 
---------update-------------
CREATE PROC hinhthucthanhtoan_update
@mahinhthucthanhtoan int,
@tenhinhthucthanhtoan nvarchar(255)
as
begin 
update hinhthucthanhtoan set tenhinhthucthanhtoan= @tenhinhthucthanhtoan
where mahinhthucthanhtoan= @mahinhthucthanhtoan
end
------------delete----------
CREATE PROC hinhthucthanhtoan_delete
	@mahinhthucthanhtoan	int
AS
	DELETE hinhthucthanhtoan WHERE mahinhthucthanhtoan = @mahinhthucthanhtoan
--------du lich get------------
create procedure hinhthucthanhtoan_get
	@Where NVARCHAR(255),
	@PageIndex INT,
	@PageSize INT,
	@OrderBy NVARCHAR(25),
	@OrderDirection NVARCHAR(4),
	@TotalRecords INT OUTPUT
as 
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
CREATE TABLE #Temp (
IDTemp INT IDENTITY(1,1),
masanpham INT
)
DECLARE @sql NVARCHAR(1000)
SET @sql = 'INSERT INTO #temp ([mahinhthucthanhtoan]) SELECT [mahinhthucthanhtoan] FROM hinhthucthanhtoan ' + @Where + ' ORDER BY ' + @OrderBy + ' ' + @OrderDirection
EXEC (@sql)
SELECT @TotalRecords = COUNT(*) FROM #Temp
SELECT
	S.mahinhthucthanhtoan,
	S.tenhinhthucthanhtoan
	
FROM #Temp AS T JOIN hinhthucthanhtoan AS S ON T.mahinhthucthanhtoan= S.mahinhthucthanhtoan
WHERE
(@PageIndex = 0)
OR
(T.IDTemp > (@PageIndex - 1) * @PageSize AND
T.IDTemp <= @PageIndex * @PageSize)
-----------khoang gia------------
---------insert------------
create procedure khoanggia_Insert
	--@makhoanggia int,
	@tenkhoanggia nvarchar(255),
	@giatoithieu int,
	@giatoida int
as
	begin 
		insert into khoanggia
		values(@tenkhoanggia,@giatoithieu,@giatoida)
	end 
--------update--------
create proc khoanggia_Update
	@makhoanggia int,
	@tenkhoanggia nvarchar(255),
	@giatoithieu int,
	@giatoida int
as
	begin
		update khoanggia
		set tenkhoanggia=@tenkhoanggia,giatoithieu=@giatoithieu,giatoida=@giatoida
		where makhoanggia=@makhoanggia
	end
----------delete---------
CREATE PROC khoanggia_delete
	@makhoanggia	int
AS
	DELETE khoanggia WHERE makhoanggia = @makhoanggia
-------- get------------
create procedure khoanggia_get
	@Where NVARCHAR(255),
	@PageIndex INT,
	@PageSize INT,
	@OrderBy NVARCHAR(25),
	@OrderDirection NVARCHAR(4),
	@TotalRecords INT OUTPUT
as 
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
CREATE TABLE #Temp (
IDTemp INT IDENTITY(1,1),
madiemden INT
)
DECLARE @sql NVARCHAR(1000)
SET @sql = 'INSERT INTO #temp ([makhoanggia]) SELECT [makhonaggia] FROM khoanggia ' + @Where + ' ORDER BY ' + @OrderBy + ' ' + @OrderDirection
EXEC (@sql)
SELECT @TotalRecords = COUNT(*) FROM #Temp
SELECT
	S.makhoanggia,
	S.tenkhoanggia,
	S.giatoithieu,
	S.giatoida
FROM #Temp AS T JOIN khoanggia AS S ON T.makhoanggia = S.makhoanggia
WHERE
(@PageIndex = 0)
OR
(T.IDTemp > (@PageIndex - 1) * @PageSize AND
T.IDTemp <= @PageIndex * @PageSize)
DROP TABLE #temp
---------lich su vi---------
-----insert--------
create proc lichsuvi_insert
--@magiaodich int,
@manguoidung int,
@maloaigiaodich int,
@sotien int,
@ngaygio datetime
as 
begin
insert into lichsuvi
values(@manguoidung,@maloaigiaodich,@sotien,@ngaygio)
end
----------update---------
create proc lichsuvi_update
@magiaodich int,
@manguoidung int,
@maloaigiaodich int,
@sotien int,
@ngaygio datetime
as

update lichsuvi set manguoidung=@manguoidung,maloaigiaodich=@maloaigiaodich,sotien=@sotien,ngaygio=@ngaygio
where magiaodich=@magiaodich
---------delete--------
create proc lichsuvi_delete
@magiaodich int
as
begin
delete lichsuvi
where magiaodich=@magiaodich
end
---------get-----------
create procedure lichsuvi_get
	@Where NVARCHAR(255),
	@PageIndex INT,
	@PageSize INT,
	@OrderBy NVARCHAR(25),
	@OrderDirection NVARCHAR(4),
	@TotalRecords INT OUTPUT
as 
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
CREATE TABLE #Temp (
IDTemp INT IDENTITY(1,1),
madiemden INT
)
DECLARE @sql NVARCHAR(1000)
SET @sql = 'INSERT INTO #temp ([magiaodich]) SELECT [magiaodich] FROM lichsuvi ' + @Where + ' ORDER BY ' + @OrderBy + ' ' + @OrderDirection
EXEC (@sql)
SELECT @TotalRecords = COUNT(*) FROM #Temp
SELECT
	S.magiaodich,
	S.manguoidung,
	S.maloaigiaodich,
	S.sotien,
	S.ngaygio
FROM #Temp AS T JOIN lichsuvi AS S ON T.magiaodich = S.magiaodich
WHERE
(@PageIndex = 0)
OR
(T.IDTemp > (@PageIndex - 1) * @PageSize AND
T.IDTemp <= @PageIndex * @PageSize)
DROP TABLE #temp
------------loaigiaodich---------
------------insert-----------
create proc loaigiaodich_insert
@maloaigiaodich int,
@tenloaigiaodich nvarchar(255)
as
insert into loaigiaodich
values(@tenloaigiaodich)
-----------update-------------
create proc loaigiaodich_update
@maloaigiaodich int,
@tenloaigiaodich nvarchar(255)
as
update loaigiaodich set tenloaigiaodich=@tenloaigiaodich
where maloaigiaodich=@maloaigiaodich
-----------delete------------
create proc loaigiaodich_delete
@maloaigiaodich int
as
delete loaigiaodich
where maloaigiaodich=@maloaigiaodich
------------get---------------
create procedure loaigiaodich_get
	@Where NVARCHAR(255),
	@PageIndex INT,
	@PageSize INT,
	@OrderBy NVARCHAR(25),
	@OrderDirection NVARCHAR(4),
	@TotalRecords INT OUTPUT
as 
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
CREATE TABLE #Temp (
IDTemp INT IDENTITY(1,1),
madiemden INT
)
DECLARE @sql NVARCHAR(1000)
SET @sql = 'INSERT INTO #temp ([maloaigiaodich]) SELECT [maloaigiaodich] FROM loaigiaodich ' + @Where + ' ORDER BY ' + @OrderBy + ' ' + @OrderDirection
EXEC (@sql)
SELECT @TotalRecords = COUNT(*) FROM #Temp
SELECT
	S.maloaigiaodich,
	S.tenloaigiaodich
FROM #Temp AS T JOIN loaigiaodich AS S ON T.maloaigiaodich = S.maloaigiaodich
WHERE
(@PageIndex = 0)
OR
(T.IDTemp > (@PageIndex - 1) * @PageSize AND
T.IDTemp <= @PageIndex * @PageSize)
DROP TABLE #temp
-------------loai nguoi dung------------
------------insert-----------
create proc loainguoidung_insert
@maloainguoidung int,
@tenloainguoidung nvarchar(255)
as
insert into loainguoidung
values(@tenloainguoidung)
-----------update-------------
create proc loainguoidung_update
@maloainguoidung int,
@tenloainguoidung nvarchar(255)
as
update loainguoidung set tenloainguoidung=@tenloainguoidung
where maloainguoidung=@maloainguoidung
-----------delete------------
create proc loainguoidung_delete
@maloainguoidung int
as
delete loainguoidung
where maloainguoidung=@maloainguoidung
drop proc loainguoidung_delete
------------get---------------
create procedure loainguoidung_get
	@Where NVARCHAR(255),
	@PageIndex INT,
	@PageSize INT,
	@OrderBy NVARCHAR(25),
	@OrderDirection NVARCHAR(4),
	@TotalRecords INT OUTPUT
as 
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
CREATE TABLE #Temp (
IDTemp INT IDENTITY(1,1),
maloainguoidung INT
)
DECLARE @sql NVARCHAR(1000)
SET @sql = 'INSERT INTO #temp ([maloainguoidung]) SELECT [maloainguoidung] FROM loainguoidung ' + @Where + ' ORDER BY ' + @OrderBy + ' ' + @OrderDirection
EXEC (@sql)
SELECT @TotalRecords = COUNT(*) FROM #Temp
SELECT
	S.maloainguoidung,
	S.tenloainguoidung
FROM #Temp AS T JOIN loainguoidung AS S ON T.maloainguoidung = S.maloainguoidung
WHERE
(@PageIndex = 0)
OR
(T.IDTemp > (@PageIndex - 1) * @PageSize AND
T.IDTemp <= @PageIndex * @PageSize)
DROP TABLE #temp
------------nguoidung---------------
------------insert-----------
create proc nguoidung_insert
@manguoidung int,
@maloainguoidung int,
@emaildangnhap nvarchar(255),
@matkhau nvarchar(255),
@sodienthoai nvarchar(255),
@diachigiaohang nvarchar(255),
@kichhoatdienthoai int,
@tongtientrongvi int,
@hinhdaidien nvarchar(255),
@gioitinh int
as
insert into nguoidung
values(@maloainguoidung,@emaildangnhap,@matkhau,@sodienthoai,@diachigiaohang,@kichhoatdienthoai,@tongtientrongvi,@hinhdaidien,@gioitinh)

-----------update-------------
create proc nguoidung_update
@manguoidung int,
@maloainguoidung int,
@emaildangnhap nvarchar(255),
@matkhau nvarchar(255),
@sodienthoai nvarchar(255),
@diachigiaohang nvarchar(255),
@kichhoatdienthoai int,
@tongtientrongvi int,
@hinhdaidien nvarchar(255),
@gioitinh int
as
update nguoidung set maloainguoidung=@maloainguoidung,emaildangnhap=@emaildangnhap,matkhau=@matkhau,sodienthoai=@sodienthoai,diachigiaohang=@diachigiaohang,kichhoatdienthoai=@kichhoatdienthoai,tongtientrongvi=@tongtientrongvi,hinhdaidien=@hinhdaidien,gioitinh=@gioitinh
where manguoidung=@manguoidung
-----------delete------------
create proc nguoidung_delete
@manguoidung int
as
delete nguoidung
where manguoidung=@manguoidung
------------get---------------
create procedure nguoidung_get
	@Where NVARCHAR(255),
	@PageIndex INT,
	@PageSize INT,
	@OrderBy NVARCHAR(25),
	@OrderDirection NVARCHAR(4),
	@TotalRecords INT OUTPUT
as 
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
CREATE TABLE #Temp (
IDTemp INT IDENTITY(1,1),
manguoidung INT
)
DECLARE @sql NVARCHAR(1000)
SET @sql = 'INSERT INTO #temp ([manguoidung]) SELECT [manguoidung] FROM nguoidung ' + @Where + ' ORDER BY ' + @OrderBy + ' ' + @OrderDirection
EXEC (@sql)
SELECT @TotalRecords = COUNT(*) FROM #Temp
SELECT
	S.manguoidung,
	S.maloainguoidung,
	S.emaildangnhap,
	S.matkhau,
	S.sodienthoai,
	S.diachigiaohang,
	S.kichhoatdienthoai,
	S.tongtientrongvi,
	S.hinhdaidien,
	S.gioitinh
FROM #Temp AS T JOIN nguoidung AS S ON T.manguoidung = S.manguoidung
WHERE
(@PageIndex = 0)
OR
(T.IDTemp > (@PageIndex - 1) * @PageSize AND
T.IDTemp <= @PageIndex * @PageSize)
DROP TABLE #temp
--------------phan quyen----------
------------insert-----------
create proc phanquyen_insert
@maphanquyen int,
@maloainguoidung int
as
insert into phanquyen
values(@maloainguoidung)
-----------update-------------
create proc phanquyen_update
@maphanquyen int,
@maloainguoidung nvarchar(255)
as
update phanquyen set maloainguoidung=@maloainguoidung
where maphanquyen=@maphanquyen
-----------delete------------
create proc phanquyen_delete
@maphanquyen int
as
delete phanquyen
where maphanquyen=@maphanquyen

--------------bo quy dinh------------
----------the end--------------
